#define _CRT_SECURE_NO_WARNINGS 1

#include<stdio.h>
#include<stdlib.h>

typedef struct MyLinkedList{
	int val;
	struct MyLinkedList* next;
} MyLinkedList;


MyLinkedList* myLinkedListCreate() {
	MyLinkedList* head = (MyLinkedList*)malloc(sizeof(MyLinkedList));
	head->next = NULL;
	head->val = 0;
	return head;
}

int myLinkedListGet(MyLinkedList* obj, int index) {
	MyLinkedList* cur = obj->next;
	for (int i = 0; cur != NULL; i++)
	{
		if (i == index)
			return cur->val;
		else
			cur = cur->next;
	}
	return -1;
}

void myLinkedListAddAtHead(MyLinkedList* obj, int val) {
	MyLinkedList* newnode = (MyLinkedList*)malloc(sizeof(MyLinkedList));
	newnode->val = val;
	newnode->next = obj->next;
	obj->next = newnode;
}

void myLinkedListAddAtTail(MyLinkedList* obj, int val) {
	MyLinkedList* cur = obj;
	while (cur->next != NULL)
	{
		cur = cur->next;
	}
	MyLinkedList* newnode = (MyLinkedList*)malloc(sizeof(MyLinkedList));
	newnode->val = val;
	newnode->next = NULL;
	cur->next = newnode;
}

void myLinkedListAddAtIndex(MyLinkedList* obj, int index, int val) {
	MyLinkedList* cur = obj->next;
	if (index==0)
	{
		myLinkedListAddAtHead(obj,val);
		return;
	}
	else
	{
		for (int i = 1; cur != NULL; i++)
		{
			if (i == index)
			{
				MyLinkedList* newnode = (MyLinkedList*)malloc(sizeof(MyLinkedList));
				newnode->val = val;
				newnode->next = cur->next;
				cur->next = newnode;
			}
			else
			{
				cur = cur->next;
			}
		}
	}
}

///** Delete the index-th node in the linked list, if the index is valid. */
//void myLinkedListDeleteAtIndex(MyLinkedList* obj, int index) {
//	if (index == 0) {
//		MyLinkedList* tmp = obj->next;
//		if (tmp != NULL) {
//			obj->next = tmp->next;
//			free(tmp);
//		}
//		return;
//	}
//	MyLinkedList* cur = obj->next;
//	for (int i = 1; cur != NULL && cur->next != NULL; i++) {
//		if (i == index) {
//			MyLinkedList* tmp = cur->next;
//			if (tmp != NULL) {
//				cur->next = tmp->next;
//				free(tmp);
//			}
//			return;
//		}
//		else {
//			cur = cur->next;
//		}
//	}
//
//}

void myLinkedListDeleteAtIndex(MyLinkedList* obj, int index) {
	if (index == 0)
	{
		MyLinkedList* tmp = obj->next;
		if (tmp != NULL)
		{
			obj->next = tmp->next;
			free(tmp);
		}
		return;
	}
	MyLinkedList* cur = obj->next;
	for (int i = 1; cur!=NULL&&cur->next != NULL; i++)
	{
		if (index == i)
		{
			MyLinkedList* tmp = cur->next;
			if (tmp != NULL)
			{
				cur->next = tmp->next;
				free(tmp);
			}
			return;
		}
		else
		{
			cur = cur->next;
		}
	}
	
}

void myLinkedListFree(MyLinkedList* obj)
{
	while (obj!= NULL)
	{
		MyLinkedList* tmp = obj;
		obj = obj->next;
		free(tmp);
	}
}

//void myLinkedListFree(MyLinkedList* obj) {
//	MyLinkedList* cur = (MyLinkedList*)malloc(sizeof(MyLinkedList));
//	cur = obj->next;
//	while (cur->next!= NULL)
//	{
//		MyLinkedList* tmp = (MyLinkedList*)malloc(sizeof(MyLinkedList));
//		tmp = cur;
//		cur = cur->next;
//		free(tmp);
//	}
//	free(obj);
//}

int main()
{
	MyLinkedList* s = myLinkedListCreate();
	myLinkedListAddAtHead(&s, 2);
	myLinkedListAddAtHead(&s, 1);
	myLinkedListAddAtTail(&s, 3);
	myLinkedListAddAtTail(&s, 4);
	myLinkedListFree(&s);
	return 0;
}